<?php

	session_start();
	
	// check for login
	if(!isset($_SESSION['atbat']) && !isset($_COOKIE['remember_atbat'])) {
	
		// no login, move to index page
		header("Location: http://localhost/atbat/html/");
	
	}
	
	// connect to db
	require_once '../db/db.php';
	
	// get user id
	$sql = "SELECT user_id FROM users WHERE MD5(username) IN ('";
	$sql .= ((isset($_SESSION['atbat'])) ? $_SESSION['atbat'] : '') . "', '";
	$sql .= ((isset($_COOKIE['remember_atbat'])) ? $_COOKIE['remember_atbat'] : '') . "')";
		
	$id = $db->query($sql)->fetchAll();
	$user_id = $id[0]['user_id'];
	
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="http://localhost/atbat/html/" />
	<meta charset="utf-8">
	<title>AtBat :: Players</title>
	<meta name="description" content="">
	<meta name="author" content="Sean Mullin, http://parametercontraption.com">
	<!--  Mobile Viewport Fix j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag -->
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
	<link rel="shortcut icon" href="images/design/favicon.ico">
	<link rel="apple-touch-icon" href="images/design/apple-touch-icon.png">
	<link rel="stylesheet" href="css/screen.css">
	<!-- For the less-enabled mobile browsers like Opera Mini --><link rel="stylesheet" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css">
    <script src="js/modernizr-1.5.min.js"></script>
</head>
<body>
	<?php require_once 'include/header.html'; ?>
    <section id="content">
    	<h1>Players</h1>
        <article>
        	<h2>Create Players</h2>
            <p>Quickly and easliy create players and rosters to use in your games. You can also choose from current MLB team rosters updated daily.</p>
            <p><a href="create-player">Create a player</a> or <a href="create-roster">roster</a> now.</p>
        </article>
        <table cellspacing="0" summary="Shows the most recently used roster of players">
        	<caption>Current Players</caption>
            <tr>
            	<th>Player</th>
                <th>#</th>
                <th>Position</th>
                <th>Bats</th>
                <th>Throws</th>
                <th>AB</th>
                <th>1B</th>
                <th>2B</th>
                <th>3B</th>
                <th>HR</th>
                <th>RBI</th>
                <th>BB</th>
                <th>K</th>
                <th>AVG</th>
                <th>SLG</th>
                <th>OPS</th>
            </tr>
            <?php
				
				$sql = "SELECT players.player_id, players.name, players.number, players.position, players.bats, players.throws, player_stats.atbats, player_stats.singles, player_stats.doubles, player_stats.triples, player_stats.home_runs, player_stats.rbi, player_stats.walks, player_stats.strikeouts, player_stats.batting_avg, player_stats.slg_avg, player_stats.obp FROM players LEFT JOIN player_stats ON player_stats.player_id = players.player_id WHERE user_id = " . $user_id;
				foreach($db->query($sql) as $row) { 
			
			?>
            <tr id="<?php echo $row[0]; ?>">
            	<td><p><a href="view-player/<?php echo $row[0]; ?>"><?php echo $row[1]; ?></a></p><p class="controls"><a href="edit-player/<?php echo $row[0]; ?>">Edit</a> | <a href="delete-player/<?php echo $row[0]; ?>" class="delete">Delete</a></p>
                </td>
                <td><?php echo $row[2]; ?></td>
                <td><?php echo $row[3]; ?></td>
                <td><?php echo $row[4]; ?></td>
                <td><?php echo $row[5]; ?></td>
                <td><?php echo $row[6]; ?></td>
                <td><?php echo $row[7]; ?></td>
                <td><?php echo $row[8]; ?></td>
                <td><?php echo $row[9]; ?></td>
                <td><?php echo $row[10]; ?></td>
                <td><?php echo $row[11]; ?></td>
                <td><?php echo $row[12]; ?></td>
                <td><?php echo $row[13]; ?></td>
                <td><?php echo $row[14]; ?></td>
                <td><?php echo $row[15]; ?></td>
                <td><?php echo $row[16]; ?></td>
            </tr>
			<?php } ?>         
            <tfoot>
            	<tr>
                	<td colspan="16">Player stats reflect only the games you have scored and not the player's actual stats.</td>
                </tr>
            </tfoot>
        </table>
    </section>
    <?php require_once 'include/footer.html'; ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <script>
    	$(document).ready(function() {
			var p_id;
			$(".delete").click(function() {
				// show delete confirm dialog
				$("#dialog-confirm").dialog("open");
				p_id = $(this).attr("href");
				p_id = p_id.split("/");
				
				return false;					
			});
			
			$("#dialog-confirm").dialog({
				resizable: false,
				draggable: false,
				autoOpen: false,
				modal: true,
				buttons: {
					"Delete Player": function() {
						// delete player via ajax
						$.post(
							"delete-player.php",
							{
								id: p_id[1],
								delete_player: true
							},
							function(data) {
								$(".error").remove();
								$(".success").remove();
								$(".notice").remove();
								$("#dialog-confirm").prepend(data);
								if($(data).hasClass('notice')) {
									// close window after 2.5 seconds on success
									setTimeout(function() { 
										$("#dialog-confirm").dialog("close");
									}, 2500);
									// remove deleted player from table
									$("#" + p_id[1]).fadeOut('slow');
								}
							}
						);
					},
					Cancel: function() {
						$(this).dialog("close");
					}
				}
			});
		});
    </script>
    <div id="dialog-confirm" title="Delete Player">
    	<p>Do you really wish to delete this player?</p>
        <p>Doing so will remove this player from your list of players, remove them from their team &amp; any rosters and delete all of their stats.</p>
    </div>
</body>
</html>